package com.lb.shopguide.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lb.shopguide.R;

/**
 * Create by kangzhao on 2018/11/26
 */
public class LbCountDownWidgetInDetail extends LinearLayout {
    private TextView tvOne;
    private TextView tvTwo;
    private TextView tvThree;
    private TextView tvFour;
    private TextView tvFive;
    private TextView tvSix;
    private TextView tvDay;
    private LinearLayout layoutDay;
    private MyCountDown countDown;

    public LbCountDownWidgetInDetail(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_detail_count_widget, this);
        tvOne = findViewById(R.id.tv_one);
        tvTwo = findViewById(R.id.tv_two);
        tvThree = findViewById(R.id.tv_three);
        tvFour = findViewById(R.id.tv_four);
        tvFive = findViewById(R.id.tv_five);
        tvSix = findViewById(R.id.tv_six);
        tvDay = findViewById(R.id.tv_day);
        layoutDay = findViewById(R.id.layout_day);
    }

    public void setTime(long millSecond) {
        countDown = new MyCountDown(millSecond, 1000);
        countDown.start();
    }

    class MyCountDown extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCountDown(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            int totalSecond = (int) (millisUntilFinished / 1000);
            int day = totalSecond / (60 * 60 * 24);
            if (day != 0) {
                tvDay.setText(day + "天");
                layoutDay.setVisibility(VISIBLE);
            } else {
                layoutDay.setVisibility(GONE);
            }
            tvOne.setText("" + ((totalSecond / (60 * 60)) % 24) / 10);
            tvTwo.setText("" + ((totalSecond / (60 * 60)) % 24) % 10);
            int minute = totalSecond % (60 * 60) / 60;
            tvThree.setText("" + minute / 10);
            tvFour.setText("" + minute % 10);
            int second = totalSecond % 60;
            tvFive.setText("" + second / 10);
            tvSix.setText("" + second % 10);
        }

        @Override
        public void onFinish() {
            if (mOnTimeOverListener != null) {
                tvOne.setText("0");
                tvTwo.setText("0");
                tvThree.setText("0");
                tvFour.setText("0");
                tvFive.setText("0");
                tvSix.setText("0");
                mOnTimeOverListener.onTimeOver();
            }
        }
    }

    public interface OnTimeOverListener {
        void onTimeOver();
    }

    private OnTimeOverListener mOnTimeOverListener;

    public void setOnTimeOverListener(OnTimeOverListener listener) {
        mOnTimeOverListener = listener;
    }
}
